package com.group.yztcedu.playlive.main.home.adapter;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.group.yztcedu.playlive.R;
import com.group.yztcedu.playlive.bean.HomeLivesBean;
import com.group.yztcedu.playlive.main.playvideo.PlayVideoActivity;
import com.group.yztcedu.playlive.utils.GlideCircleTransform;
import com.group.yztcedu.playlive.utils.GlideRoundTransform;

import java.util.List;


public class HomeRecommendAdapter extends BaseAdapter {
    public List<HomeLivesBean> mList;
        public HomeRecommendAdapter(List<HomeLivesBean> list){
            this.mList  = list;
        }
    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int i) {
        return mList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, final ViewGroup viewGroup) {
        ViewHolder holder;
        if(view==null){
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.home_gridview_fire, null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }else{
            holder = (ViewHolder) view.getTag();
        }
        HomeLivesBean homeLivesBean = mList.get(i);
        String snapshotUrl = homeLivesBean.getSnapshot();
        String avatarUrl = homeLivesBean.getUser().getAvatar();
        String username = homeLivesBean.getUser().getUsername();
        int liveUserCount = homeLivesBean.getLiveUserCount();
        String title = homeLivesBean.getTitle();
        holder.fireTv.setText(username);
        holder.fireUserCont.setText(""+liveUserCount);
        holder.fireTitle.setText(title);
        Glide.with(viewGroup.getContext()).load(snapshotUrl).transform(new GlideRoundTransform(viewGroup.getContext())).into(holder.imageIv);
        Glide.with(viewGroup.getContext()).load(avatarUrl).transform(new GlideCircleTransform(viewGroup.getContext())).into(holder.logoIv);
        holder.imageIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int roomId = mList.get(i).getRoomId();
                Intent intent = new Intent(viewGroup.getContext(), PlayVideoActivity.class);
                intent.putExtra("roomId",roomId+"");
                viewGroup.getContext().startActivity(intent);
            }
        });
        return view;

    }

    class ViewHolder{
        private ImageView logoIv,imageIv;
        private TextView fireTv,fireUserCont,fireTitle;
        public ViewHolder(View view){
             imageIv = (ImageView) view.findViewById(R.id.home_frame_fire_iv);
            logoIv = (ImageView) view.findViewById(R.id.home_fire_iv);
           fireTv  = (TextView) view.findViewById(R.id.home_fire_tv);
            fireUserCont  = (TextView) view.findViewById(R.id.home_fire_userCount_tv);
            fireTitle  = (TextView) view.findViewById(R.id.home_fire_title);
        }
    }
}
